﻿// JScript 文件
//获得文档中真实尺寸

//页面宽度
document.getOffsetWidth = function() {
    if (document.IsIE && document.compatMode == "BackCompat") {
        return document.body.offsetWidth;
    } else {
        return document.documentElement.offsetWidth;
    }
}

//页面高度
document.getOffsetHeight=function(){
if (document.IsIE && document.compatMode == "BackCompat") {
        return document.body.offsetHeight;
    }else{
        return document.documentElement.offsetHeight;
    }
}


//滚动条宽度
document.getScrollWidth = function() {
    if (document.IsIE && document.compatMode == "BackCompat") {
        return document.body.scrollWidth;
    } else {
        return document.documentElement.scrollWidth;
    }
}

//滚动条高度
document.getScrollHeight = function() {
    if (document.IsIE && document.compatMode == "BackCompat") {
        return document.body.scrollHeight;
    } else {
        return document.documentElement.scrollHeight;
    }
}

//滚动条卷入的宽度
document.getScrollLeft = function() {
    if (document.IsIE && document.compatMode == "BackCompat") {
        return document.body.scrollLeft;
    } else {
        return document.documentElement.scrollLeft;
    }
}

//滚动条卷入的高度
document.getScrollTop = function() {
    if (document.IsIE && document.compatMode == "BackCompat") {
        return document.body.scrollTop;
    } else {
        return document.documentElement.scrollTop;
    }
}

//显示内容区域（不含边框）的宽度
document.getClientWidth = function() {
    if (document.IsIE && document.compatMode == "BackCompat") {
        return document.body.clientWidth;
    } else {
        return document.documentElement.clientWidth;
    }
}

//显示内容区域（不含边框）的高度
document.getClientHeight = function() {
    if(window.innerHeight) return window.innerHeight;
    if (document.IsIE && document.compatMode == "BackCompat") {
        return document.body.clientHeight;
    } else {
        return document.documentElement.clientHeight;
    }
}


//获得页面元素的真实左侧位置
document.getObjLeft=function(obj){
    var left=obj.offsetLeft;
    while(obj=obj.offsetParent){
        left+=obj.offsetLeft;
    }
    return left;
}

//获得页面元素的真实顶部位置
document.getObjTop=function(obj){
    var top=obj.offsetTop;
    while(obj=obj.offsetParent){
        top+=obj.offsetTop;
    }
    return top;
}

document.mouseX=function(event){
	if(!event) event=window.event;
	return event.clientX+document.getScrollLeft();
	
}
document.mouseY=function(event){
	if(!event) event=window.event;
	return event.clientY+document.getScrollTop();
}

document.addElement = function(parentNode, tag, name, type) {
    var d = null;
    if (document.all) {
        if (!name && !type) {
            d = document.createElement(tag);
        } else {
            var str = "<" + tag
            if (name) str += " name=" + name;
            if (type) str += " type=" + type;
            str += ">";
            d = document.createElement(str);
        }
    } else {
        d = document.createElement(tag);
        if (name) d.name = name;
        if (name) d.type = type;
    }

    if (parentNode.hasChildNodes()) parentNode.insertBefore(d, parentNode.firstChild);
    else parentNode.appendChild(d);
    return d;
}

document.addElementAtEnd = function(parentNode, tag, name, type) {
    var d = null;
    if (document.all) {
        if (!name && !type) {
            d = document.createElement(tag);
        } else {
            var str = "<" + tag
            if (name) str += " name=" + name;
            if (type) str += " type=" + type;
            str += ">";
            d = document.createElement(str);
        }
    } else {
        d = document.createElement(tag);
        if (name) d.name = name;
        if (name) d.type = type;
    }
    parentNode.appendChild(d);
    return d;
}

document.delElement=function(obj){
    var parentNode=obj.parentNode;
    if(parentNode){
        for(var i=obj.childNodes.length-1;i>=0;i--){
            document.delElement(obj.childNodes[i]);
        }
        var s=parentNode.removeChild(obj);
        delete s;s=null;
    }
    delete obj;obj=null;
    delete parentNode;parentNode=null;
}

//使一个对象相对于窗口居中,显示时才可操作
document.toCenter=function(obj){
	var clientWidth=document.getClientWidth();
	var clientHeight=document.getClientHeight();
	var scrollLeft=document.getScrollLeft();
	var scrollTop=document.getScrollTop();
	var objWidth=obj.offsetWidth;
	var objHeight=obj.offsetHeight;
	var objLeft=((clientWidth-objWidth)/2+scrollLeft);
	var objTop=((clientHeight-objHeight)/2+scrollTop);
	if(objLeft<0) objLeft=0;
	if(objTop<0) objTop=0;
	obj.style.left=objLeft+"px";
	obj.style.top=objTop+"px";
}

//将对象置于窗口的某一固定位置,,显示时才可操作
//position={left:1,top:1,bottom:1,right:1}至少要提供一个坐标点形式的参数。
document.toFixedPosition=function(obj,position){
    obj.style.position="fixed";
    var xAxisAble=false,yAxisAble=false;
    if(position.left!=null){
        obj.style.left=position.left+"px";
        if(obj.offsetLeft==position.left) xAxisAble=true;
    }
    if(position.right!=null){
        obj.style.right=position.right+"px";
        if(document.getClientWidth()-obj.offsetLeft-obj.offsetWidth==position.right) xAxisAble=true;
    }
    if(position.top!=null){
        obj.style.top=position.top+"px";
        if(obj.offsetTop==position.top) yAxisAble=true;
    }
    if(position.bottom!=null){
        obj.style.bottom=position.bottom+"px";
        if(document.getClientHeight()-obj.offsetTop-obj.offsetHeight==position.bottom) yAxisAble=true;
    }
    if(!xAxisAble||!yAxisAble){
        obj.style.position="absolute";
        if(document.toFixedPosition____setPosition){
            EventOperate.delEvent(window,"scroll",document.toFixedPosition____setPosition);
            EventOperate.delEvent(window,"resize",document.toFixedPosition____setPosition);
        }
        document.toFixedPosition____setPosition=function(){
            if(position.left!=null){
                obj.style.left=(document.getScrollLeft()+position.left)+"px";
            }
            if(position.right!=null){
                obj.style.left=(document.getScrollLeft()+document.getClientWidth()-position.right-obj.offsetWidth)+"px";
            }
            if(position.top!=null){
                obj.style.top=(document.getScrollTop()+position.top)+"px";
            }
            if(position.bottom!=null){
                obj.style.top=(document.getScrollTop()+document.getClientHeight()-position.bottom-obj.offsetHeight)+"px";
            }
        }
        EventOperate.addEvent(window,"scroll",document.toFixedPosition____setPosition);
        EventOperate.addEvent(window,"resize",document.toFixedPosition____setPosition);
        document.toFixedPosition____setPosition();
       
    }
}

document.toFixedCenter=function(obj){
    document.toFixedCenter___setPosition=function(){
        var left=Math.floor((document.getClientWidth()-obj.offsetWidth)/2);
        var top=Math.floor((document.getClientHeight()-obj.offsetHeight)/2);
        document.toFixedPosition(obj,{left:left,top:top});
    }
    EventOperate.addEvent(window,"resize",document.toFixedCenter___setPosition);
    document.toFixedCenter___setPosition();
}

//让除Dialog中的元素，其他元素禁止操作
document.toBack=function(istrue){
    if(!document.___baockIframe){
        document.___baockDiv=document.addElement(document.body,"div");
        document.setStyle(document.___baockDiv,{opacity:0.4,position:"absolute",backgroundColor:"#ccc",top:0,left:0,display:"none"});
        EventOperate.FixedPart(document.___baockDiv);
        document.___baockIframe=document.addElement(document.body,"iframe");
        document.___baockIframe.frameBorder=0;
        document.___baockIframe.setAttribute("frameborder", "0", 0);    
        document.___baockIframe.style.filter="alpha(opacity=0)";
        document.___baockIframe.style.opacity=0;
        document.___baockIframe.style.position="absolute";
        document.___baockIframe.style.backgroundColor="#ccc";
        document.___baockIframe.style.top=0;
        document.___baockIframe.style.left=0;
        document.___baockIframe.scrolling="no";
        document.___baockIframe.style.display="none";
        EventOperate.FixedPart(document.___baockIframe);
        EventOperate.addEvent(window,"resize",document.toBack______setSize);
    }
    document.boBack_______istrue=istrue;
    if(istrue){
        document.___baockIframe.style.display="block";
        document.___baockDiv.style.display="block";
        document.setWidth(document.___baockIframe,Math.max(document.getScrollWidth(),document.getClientWidth()));
        document.setHeight(document.___baockIframe,Math.max(document.getScrollHeight(),document.getClientHeight()));
            document.setWidth(document.___baockDiv,Math.max(document.getScrollWidth(),document.getClientWidth()));
        document.setHeight(document.___baockDiv,Math.max(document.getScrollHeight(),document.getClientHeight()));
    }else{
        document.___baockIframe.style.display="none";
        document.___baockDiv.style.display="none";
    }
}

document.toBack______setSize=function(){
    if(document.boBack_______istrue){
        document.setWidth(document.___baockIframe,Math.max(document.getScrollWidth(),document.getClientWidth()));
        document.setHeight(document.___baockIframe,Math.max(document.getScrollHeight(),document.getClientHeight()));
        document.setWidth(document.___baockDiv,Math.max(document.getScrollWidth(),document.getClientWidth()));
        document.setHeight(document.___baockDiv,Math.max(document.getScrollHeight(),document.getClientHeight()));
    }
}

document.isToBack=function(){
    if(boBack_______istrue) return true;
    return false;
}

//设置元素的高度 边框+padding+内容 的宽度,显示时才可操作
//isHidden::多出部分是
document.setHeight=function(obj,height,isHidden){
    obj.style.height=height+"px";
    if(isHidden) obj.style.overflow="hidden";
    else obj.style.overflow="auto";
    if(obj.offsetHeight>height){
        obj.style.height=(height-(obj.offsetHeight-height))+"px";
    }
}

//设置元素的高度 边框+padding+内容 的宽度,显示时才可操作
//isHidden::多出部分是
document.setWidth=function(obj,width,isHidden){
    obj.style.width=width+"px";
    if(isHidden) obj.style.overflow="hidden";
    else obj.style.overflow="auto";
    if(obj.offsetWidth>width){
        obj.style.width=(width-(obj.offsetWidth-width))+"px";
    }
}

//给DOM添加一个Css样式
document.addClass=function(obj,className){
	if(!className) return;
	var classes=document.getClasses(obj);
	classes.push(className);
	obj.className=classes.join(" ");
	delete obj;
	obj=null;
}

//给DOM去除一个Css样式,className为空表示清除所有样式
document.removeClass=function(obj,className){
	if(!className) obj.className="";
	var classes=document.getClasses(obj);
	classes.remove(className);
	obj.className=classes.join(" ");
	delete obj;
	obj=null;
}

//取得样式名称列表
document.getClasses=function(obj){
    var c=obj.className;
	var classes=[];
	if(c!=null&&c!="") classes=c.split(" ");
	return classes;
}

//测试是否包含某个样式
document.hasClass=function(obj,className){
    if(!className) return false;
    var classes=document.getClasses(obj);
    return (classes.indexOf(className)!=-1)
}

//给Dom添加style，推荐使用style对象
//{color:"red",float:"left"}
//透明度用“opacity”,值为0-1之间小数
document.setStyle=function(obj,style){
	if(typeof(style)==="string"){
		obj.style.cssText=style;
		obj.style.styleText=style;
	}else{
		for(var i in style){
			if(i==="cssFloat"||i==="styleFloat"){
				obj.style["cssFloat"]=style[i];
				obj.style["styleFloat"]=style[i];
			}else if(i==="opacity"){
				obj.style["opacity"]=style[i];
				obj.style["filter"]="alpha(opacity="+style[i]*100+")";
			}else{
				obj.style[i]=style[i];
			}
		}
	}
	delete obj;
	obj=null;
	delete style;
	style=null;
}

//绑定数据到页面
document.bindData=function(temp,data){
    if(typeof(temp)=="string") temp=$$(temp);
    var parent=temp.parentNode;
    var nodes=parent.childNodes;
    //清除,取得模板
    var item;
    for(var i=nodes.length-1;i>=0;i--){
        var node=nodes[i];
        if(node.nodeType==1 && node.getAttribute("key")=="copy_item"){
            parent.removeChild(node);
        }
    }
    if(data.constructor==Array){
        var fra=document.createDocumentFragment();
        for(var i=0,l=data.length;i<l;i++){
            var clone=temp.cloneNode(true);
            clone.setAttribute("key","copy_item");
            clone.style.display="";
            clone.id=temp.id+"__"+i;
            fra.appendChild(clone);
            document.__bindData__(temp,clone,data[i]);
        }
        parent.insertBefore(fra,temp);
    }
}

document.__bindData__=function(temp,clone,item){
    clone.outerHTML=document.__bindData__replaceValue(unescape(clone.outerHTML),item);
}

document.__bindData__replaceValue=function(text,item){
    var test =/\{\$(\*?\w+?)(,(.+?))?\$\}/g;
    return text.replace(test,function($0,$1,$2,$3){
        var v=item[$1];
        if(v==null) return "";
        if($3!=null&&$3!="") return v.toString($3);
        else return v;
    });
}

document.layerGoodInIE6=function(obj){
	if(!obj.__1___1___1___1___)	obj.__1___1___1___1___=document.addElement(obj,"iframe");
	var width=obj.offsetWidth;
	var height=obj.offsetHeight;
	document.setStyle(obj.__1___1___1___1___,{opacity:0,color:"red",zIndex:-1,position:"absolute",width:width+"px",height:height+"px",left:0,top:0});
}
